import {getRecinvoicesList,approval,receiveinvoice, checkinvoice } from '../../services/drawback/recinvoices';
import {getInvoicesList, preinvoiceopen, inoviceheader, inoviceproducts} from '../../services/drawback/invoice';
import Constants from '../../utils/Constants';
import {parse} from 'qs';
import { message } from 'antd';
export default {
    namespace: 'recinvoices',
    state: {
        search: { // 查询条件
            orderno: '',
            userIds: '',
            openinvoicestatus: '2'
        },
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        list: [],
        editModel: { // 编辑视图
            showStatus: false, // 默认不显示
            confirmLoading: false, // 确认按钮loading状态
            title: '预开票', // 标题
            form: {
                defaultWidth: 1000, // 缺省宽度800
                width: 1000, // 宽度
                preamount: '',
                amountcurrency: '',
                precharge: '',
                chargecurrency: '',
                prerate: '',
                orderId: '',
                step : 0
            }
        }
    },
    reducers: {

        /**
         * 设置查询条件
         *
         * @param state
         * @param actio
         * @returns {{search}}
         */
        setSearch(state, action) {
            return {...state, search: action.payload.search};
        },

        /**
         * 是否显示
         *
         * @param state
         * @param action
         * @returns {{showStatus: (boolean|*)}}
         */
        setEditModel(state, action) {
            let payload = action.payload;

            let newState = {...state.editModel};
            if (payload.showStatus != undefined) {
                newState.showStatus = payload.showStatus;
            }
            if (payload.title != undefined) {
                newState.title = payload.title;
            }
            if (payload.confirmLoading != undefined) {
                newState.confirmLoading = payload.confirmLoading;
            }

            if(payload.type != undefined) {
                newState.type = payload.type;
            }
            if(payload.form != undefined) {
                newState.form = payload.form;
            }

            if(payload.orderId != undefined) {
                newState.orderId = payload.orderId;
            }

            if(payload.step != undefined) {
                newState.step = payload.step;
            }

            if(payload.width != undefined) {
                newState.width = payload.width;
            }

            if(payload.form != undefined) {
                newState.form = payload.form;
            }

            return {...state, editModel: {...newState}};
        },

        /**
         * 设置数据列表
         *
         * @param state
         * @param action
         */
        setList(state, { payload }) {
            return {...state, list: payload.list, page: payload.page}
        },

        /**
         * 设置页码
         *
         * @param state
         * @param payload
         * @returns {{page: *}}
         */
        setPage(state, { payload }) {
            return {...state, page: payload.page}
        }
    },
    effects: {

        /**
         * 分页查询收票
         * @param call
         * @param put
         * @param select
         */
            *getRecinvoicesList({}, {call, put, select}) {
            const search = yield select(state => state.recinvoices.search);
            const page = yield select(state => state.recinvoices.page);


            const res = yield call(getRecinvoicesList, {
                orderno: search.orderno,
                userIds: search.userIds,
                openinvoicestatus: search.openinvoicestatus,
                page
            });


            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setList',
                    payload: {
                        list: content.list,
                        page: content.page == undefined ? Constants.DEFAULTPAGE : content.page
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 根据订单获取开票资料表头信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *inoviceheader({ payload }, { call, put, select }) {
            const res = yield call(inoviceheader, { orderId: payload.orderId});
            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'inoviceproducts',
                    payload: {
                        orderId : payload.orderId,
                        form: {
                            heard : content[0],
                            type : payload.type,
                        },
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         *根据订单获取开票资料具体产品信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *inoviceproducts({ payload }, { call, put, select }) {
            const res = yield call(inoviceproducts, { orderId: payload.orderId});

            if (res.data.resultCode === "ok") {
                let content = res.data.content;

                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: true,
                        confirmLoading: false,
                        title: "收票详情",
                        step: 1,
                        width: 1500,
                        form: {
                            heard : payload.form.heard,
                            type : payload.form.type,
                            list : content,
                            list2 : payload.form.heard == null ? null : payload.form.heard.taxInvoices,
                            checkstatus: payload.form.heard.taxInvoices == null || payload.form.heard.taxInvoices.length == 0 ?
                                null : payload.form.heard.taxInvoices[0].checkstatus,
                            memo: payload.form.heard.taxInvoices == null || payload.form.heard.taxInvoices.length == 0 ?
                                null : payload.form.heard.taxInvoices[0].memo,

                        }
                    }
                    });

            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加发票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *addInvoices({ payload }, { call, put, select }) {
            let num = payload.num;

            if (payload.list2 != undefined && num != undefined && payload.receiptno != undefined) {
                for (let i = 0; i < payload.list2.length; i++) {
                    let receiptno = payload.list2[i].receiptno;
                    let tagreceiptno = payload.receiptno;
                    for (let i = 0; i < num; i++) {
                        if (receiptno == tagreceiptno) {
                            message.error("发票号已存在");

                            yield put({
                                type: 'setEditModel',
                                payload: {
                                    showStatus: true,
                                    confirmLoading: false,
                                    title: "收票详情",
                                    step: 1,
                                    width: 1500,
                                    form: {
                                        list: payload.list,
                                        heard: payload.heard,
                                        type: payload.type,
                                        list2: payload.list2,
                                    }
                                }
                            });

                            return;
                        }
                        ++tagreceiptno;
                    }

                }
            }

            let list2 = (payload.list2 != undefined ? payload.list2 : []);

            if (num != undefined) {
                let receiptno = payload.receiptno;
                for (let i = 0; i < num; i++) {
                    let invoices = {
                        receiptno: receiptno,
                        drawername: payload.drawername,
                        createtime: payload.createtime,
                        orderid: payload.heard.orderId,
                    };

                    ++receiptno;
                    list2.push(invoices);
                }
            }

            yield put({
                type: 'setEditModel',
                payload: {
                    showStatus: true,
                    confirmLoading: false,
                    title: "收票详情",
                    step: 1,
                    width: 1500,
                    form: {
                        list: payload.list,
                        heard: payload.heard,
                        type: payload.type,
                        list2: list2,
                    }
                }
            });
        },


        /**
         * 删除发票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *deleteInvoices({ payload }, { call, put, select }) {

            let deleteReceiptno = null;
            if (payload.list2 != undefined  && payload.receiptno != undefined) {
                for (let i = 0; i < payload.list2.length; i++) {
                    let receiptno = payload.list2[i].receiptno;
                    let tagreceiptno = payload.receiptno;

                    if(receiptno == tagreceiptno){
                        deleteReceiptno = i;
                    }

                }
            }

            if(deleteReceiptno != null){
                // payload.list2.pop(deleteReceiptno);
                delete payload.list2[deleteReceiptno]
            }

            yield put({
                type: 'setEditModel',
                payload: {
                    showStatus: true,
                    confirmLoading: false,
                    title: "收票详情",
                    step: 1,
                    width: 1500,
                    form: {
                        list: payload.list,
                        heard: payload.heard,
                        type: payload.type,
                        list2: payload.list2,
                    }
                }
            });
        },

        /**
         * 收票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *receiveinvoice({ payload }, { call, put, select }) {

            let form ={
                orderId :payload.orderId,
                taxInvoiceFormList :payload.list2,
            };


            const res = yield call(receiveinvoice, {form});
            if (res.data.resultCode === "ok") {
                message.info("收票成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getRecinvoicesList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 审核
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *approval({ payload }, { call, put, select }) {
            const res = yield call(approval, payload);
            if (res.data.resultCode === "ok") {
                message.info("提交审核成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getRecinvoicesList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 认证发票
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *checkinvoice({ payload }, { call, put, select }) {
            const res = yield call(checkinvoice, payload);
            if (res.data.resultCode === "ok") {
                message.info("提交认证成功！");
                yield put({
                    type: 'setEditModel',
                    payload: {
                        showStatus: false
                    }
                });
                yield put({
                    type: 'getRecinvoicesList'
                });
            } else {
                message.error(res.data.errMessage);
            }
        },



    },
    subscriptions: {
        setup({history, dispatch}) {
            return history.listen(({pathname}) => {
                if(pathname.includes('drawback/clearanceApproval')) {
                    dispatch({
                        type: 'getRecinvoicesList'
                    });
                }

            });
        }
    }

};
